package com.happyfamily.springboot.crowdsourcingplatform.service.wang.impl;

import com.happyfamily.springboot.crowdsourcingplatform.base.result.Results;
import com.happyfamily.springboot.crowdsourcingplatform.dao.wang.RuleDao;
import com.happyfamily.springboot.crowdsourcingplatform.model.Rule;
import com.happyfamily.springboot.crowdsourcingplatform.model.SysUser;
import com.happyfamily.springboot.crowdsourcingplatform.service.wang.RuleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


//曝光台实现类
@Service
@Transactional
@Slf4j
public class RuleServiceImpl implements RuleService
{
	@Autowired
	private RuleDao ruleDao;

	//前端规则中心显示
	@Override
	public List<Rule> ruleList()
	{
		return ruleDao.ruleList();
	}

	//后端对规则的删除
	@Override
	public String ruleDelete(String rid)
	{
		int index=ruleDao.ruleDelete(rid);
		String res="error";
		if (index>0)
		{
			res="success";
		}
		return res;
	}

	//规则修改前的回显
	@Override
	public Rule ruleReview(String rid)
	{
		return ruleDao.ruleReview(rid);
	}

	//管理员新增规则
	@Override
	public String ruleAdd(String titles, String contents, HttpServletRequest req)
	{
		int index=ruleDao.titleCount(titles);
		String res="error";
		if (index<=0)
		{
			SecurityContextImpl securityContext=(SecurityContextImpl)req.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
			String uaccount=securityContext.getAuthentication().getName();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
			String rtime=sdf.format(new Date());// new Date()为获取当前系统时间
			index=ruleDao.ruleAdd(titles,contents,rtime,uaccount);
			if (index>0)
			{
				res="success";
			}
		}
		return res;
	}

	//管理员修改规则
	@Override
	public String ruleUpdata(String rid,String titles,String contents,String backTitle, HttpServletRequest req)
	{
		int index=0;
		if (!titles.equals(backTitle))
		{
			index=ruleDao.titleCount(titles);
		}
		String res="error";
		if (index<=0)
		{
			res="not";
			SecurityContextImpl securityContext=(SecurityContextImpl)req.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
			String uaccount=securityContext.getAuthentication().getName();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
			String rtime=sdf.format(new Date());// new Date()为获取当前系统时间
			index=ruleDao.ruleUpdata(rid,titles,contents,rtime,uaccount);
			if (index>0)
			{
				res="success";
			}
		}
		return res;
	}

	//后端规则表格显示
	@Override
	public Results<Rule> ruleTable(Integer startPosition, Integer limit, String uname, String rtitle, String rcontent, String rtime)
	{
		return Results.success(ruleDao.ruleCount(uname,rtitle,rcontent,rtime).intValue(),ruleDao.ruleTable(startPosition,limit,uname,rtitle,rcontent,rtime));
	}
}
